package com.example.graduationdesignbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.MerchantDto;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.Merchant;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.MerchantMapper;
import com.example.graduationdesignbackend.query.MerchantQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IMerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 园区周边 服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-04-21
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {

    @Override
    public Res<String> createMerchant(MerchantDto merchantDto) {
        Merchant merchant = new Merchant();
        merchant.setId(RandomUtil.randomNumbers(9));
        merchant.setTitle(merchantDto.getTitle());
        if(!BeanUtil.isEmpty(merchantDto.getDescribeInfo())){
            merchant.setDescribeInfo(merchantDto.getDescribeInfo());
        }
        if (!BeanUtil.isEmpty(merchantDto.getAddress())){
            merchant.setAddress(merchantDto.getAddress());
        }
        merchant.setLatitude(merchantDto.getLatitude());
        merchant.setLongitude(merchantDto.getLongitude());
        merchant.setBusinessHours(merchantDto.getBusinessHours());
        merchant.setContacts(merchantDto.getContacts());
        merchant.setTelephone(merchantDto.getTelephone());
        merchant.setTypeCode("jd");
        merchant.setTypeName("景点");
        merchant.setStatus(9);
        if (!BeanUtil.isEmpty(merchantDto.getHeadImage())){
            merchant.setHeadImage(merchantDto.getHeadImage());
        }
        merchant.setCreateTime(new Date().getTime());
        boolean save = save(merchant);
        if (!save){
            throw new ParkException(ResponseCode.SaveError);
        }
        return Res.yuze("创建成功！");
    }

    @Override
    public Res<String> editMerchant(MerchantDto merchantDto) {
        Merchant merchant = getById(merchantDto.getId());
        merchant.setTitle(merchantDto.getTitle());
        if(!BeanUtil.isEmpty(merchantDto.getDescribeInfo())){
            merchant.setDescribeInfo(merchantDto.getDescribeInfo());
        }
        if (!BeanUtil.isEmpty(merchantDto.getAddress())){
            merchant.setAddress(merchantDto.getAddress());
        }
        merchant.setLatitude(merchantDto.getLatitude());
        merchant.setLongitude(merchantDto.getLongitude());
        merchant.setBusinessHours(merchantDto.getBusinessHours());
        merchant.setContacts(merchantDto.getContacts());
        merchant.setTelephone(merchantDto.getTelephone());
        merchant.setStatus(9);
        merchant.setTypeCode("jd");
        merchant.setTypeName("景点");
        if (!BeanUtil.isEmpty(merchantDto.getHeadImage())){
            merchant.setHeadImage(merchantDto.getHeadImage());
        }
        merchant.setUpdateTime(new Date().getTime());
        Boolean res = updateById(merchant);
        if (!res){
            throw new ParkException(ResponseCode.UpdateError);
        }
        return Res.yuze("编辑成功！");
    }

    @Override
    public PageDto<Merchant> getMerchantPage(MerchantQuery merchantQuery) {
        String key = merchantQuery.getKey();
        int current = 1;
        if (!BeanUtil.isEmpty(merchantQuery.getCurrent())){
            current = merchantQuery.getCurrent();
        }
        int pageSize = 10;
        if (!BeanUtil.isEmpty(merchantQuery.getPageSize())){
            pageSize = merchantQuery.getPageSize();
        }
        Page<Merchant> page = Page.of(current,pageSize);
        page.addOrder(new OrderItem("create_time",false));
        Page<Merchant> p = new Page<>();
        if (BeanUtil.isEmpty(merchantQuery.getKey())){
            p = lambdaQuery().page(page);
        }else {
            p = lambdaQuery()
                    .like(Merchant::getTitle,key)
                    .or()
                    .like(Merchant::getContacts,key)
                    .page(page);
        }

        PageDto<Merchant> dto = new PageDto<>();
        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());
        dto.setPageSize(p.getSize());
        dto.setCurrent(p.getCurrent());
        dto.setList(p.getRecords());
        return dto;
    }

    @Override
    public List<Merchant> getMerchantList() {
        return  lambdaQuery().eq(Merchant::getStatus,9).list();
    }
}
